package gui.visu.listeners;

import gui.utils.IntegerField;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import tuio.components.SliderTouch;


/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class ListenerIntegerField extends KeyAdapter{

	private SliderTouch linkedSlider;
	private IntegerField field;

	public ListenerIntegerField(IntegerField field,SliderTouch linkedSlider){
		this.setLinkedSlider(linkedSlider);
		this.setField(field);
	}

	public ListenerIntegerField(){
		this.setLinkedSlider(null);
	}
	
	@Override
	public void keyReleased(KeyEvent ke) {
		String typed = field.getText();
		linkedSlider.setValue(0);
		if(!typed.matches("\\d+") || typed.length() > 3) {
			return;
		}
		int value = Integer.parseInt(typed);
		linkedSlider.setValue(value);
	}
	
	

	public void setLinkedSlider(SliderTouch linkedSlider) {
		this.linkedSlider = linkedSlider;
	}

	public SliderTouch getLinkedSlider() {
		return linkedSlider;
	}

	public void setField(IntegerField field) {
		this.field = field;
	}

	public IntegerField getField() {
		return field;
	}



}
